// Vuex持久化保持方案
import createPersistedState from 'vuex-persistedstate'
// Vuex 自带日志插件
// import createLogger from 'vuex/dist/logger'
import { Cache } from '../cache/index'
import { isObject, isString } from '../utils/index'

export default {
	state: {
		userinfo: {},
		subject: {},
		subject2: {},
		subject3: {},
		subjectVip: '',//科目vip信息
		//微信网页授权登录开始
		loginStatus: 0, //登录状态,
		appId: '',//appid 微信网页授权获取appid
		//微信网页授权登录结束
	},
	action: {},
	getters: {
		//微信网页授权登录开始
		login_Status(state) {
			return state.loginStatus
		},
		user_Info(state) {
			return state.userinfo;
		},
		appId(state) {
			return state.appId
		},
		//微信网页授权登录结束
	},
	mutations: {
		setSubjectVipInfo(state, options) {
			state.subjectVip = options
		},
		//微信网页授权登录开始
		setLoginStatus(state, options) {
			state.loginStatus = options
		},
		setAppId(state, options) {
			state.appId = options
		},
		//微信网页授权登录结束
		setSubject(state, payload) {
			if (payload && isObject(payload) && payload.subject) {
				state.subject = payload.subject
				state.subject2 = payload.subject2
				state.subject3 = payload.subject3
			}
		},

		setSubject2(state, payload) {
			if (payload && isObject(payload) && payload.subject2) {
				state.subject2 = payload.subject2
			}
		},

		setSubject3(state, payload) {
			if (payload && isObject(payload) && payload.subject3) {
				state.subject3 = payload.subject3
			}
		},

		setUserInfo(state, payload) {
			if (payload && isObject(payload) && payload.userinfo) {
				state.userinfo = payload.userinfo
			}
		},
		updateUserInfo(state, { avatar, nickname }) {
			// 更新 state 中的 userinfo 对象
			state.userinfo = {
				...state.userinfo,
				avatarUrl: avatar,
				nickName: nickname,
			};
		},
		clearUserInfo(state, payload) {
			state.userinfo = {}
		}
	},
	modules: {},
	plugins: [
		// createLogger,
		createPersistedState({
			key: '',
			storage: new Cache()
		})
	]
}
